﻿using PublicClass;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace PublicScreenMenue
{
    // 屏幕菜单文件交互类
    public class ScreenMenu
    {
        private static ScreenMenu screenMenu = new ScreenMenu();
        // 分组
        private List<ItemDataGroup> Groups = null;

        public string RootName { get; set; }

        static public ScreenMenu MenuData()
        {
            return screenMenu;
        }
        private ScreenMenu()
        {
            Load(PublicNoCad.GetSysData() + "Menu.xml");
        }
        private bool Load(string file)
        {
            if (File.Exists(file))
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(file);
                XmlNode root = doc.SelectSingleNode("ScreenMenu");
                RootName = "菜单";
                XmlAttribute att = root.Attributes["Title"];
                if (null != att)
                    RootName = att.Value;
                this.DoChangeData(root, true);
            }

            return true;
        }


        bool Save()
        {
            return true;
        }

        void DoChangeData(XmlNode node, bool bLoad)
        {
            Groups = new List<ItemDataGroup>();
            XmlNodeList nodeList = node.ChildNodes;
            foreach (XmlNode nodeItem in nodeList)
            {
                ItemDataGroup group = new ItemDataGroup();
                group.DoChangeData(nodeItem, true);
                Groups.Add(group);
            }
        }

        public List<ItemDataGroup> GetGroupList()
        {
            return Groups;
        }
    }

    /// <summary>
    /// 一个菜单按钮
    /// </summary>
    public class MenuItemData
    {
        public string Text;
        public string Command;

        public string PngImage;
        public string TextColor;
        public string Describe;
        public bool RibbonUserBigPng;
        /// <summary>
        /// 分隔符
        /// </summary>
        /// <returns></returns>
        public bool IsSep()
        {
            return Command.Length == 0;
        }
        public MenuItemData()
        {
            Text = "";
            Command = "";
            PngImage = "";
            TextColor = "";
            Describe = "";
            RibbonUserBigPng = true;
        }
        public void DoChangeData(XmlNode node, bool bLoad)
        {
            XmlAttribute att = node.Attributes["Text"];
            if (null != att)
                Text = att.Value;
            att = node.Attributes["Command"];
            if (null != att)
                Command = att.Value;
            att = node.Attributes["PngImage"];
            if (null != att)
                PngImage = att.Value;
            att = node.Attributes["TextColor"];
            if (null != att)
                TextColor = att.Value;
            att = node.Attributes["Describe"];
            if (null != att)
                Describe = att.Value;
            att = node.Attributes["RibbonUserBigPng"];
            if (null != att)
                RibbonUserBigPng = Convert.ToBoolean(att.Value);
        }

        public string GetImgFilePath(bool bBig = true)
        {
            string str = System.IO.Path.Combine(PublicNoCad.GetSysData(), "Img");
            str = System.IO.Path.Combine(str, bBig ? "BigImg" : "SmallImg");
            str = System.IO.Path.Combine(str, PngImage);

            return str;
        }
        public System.Drawing.Image SmallImage()
        {
            string strImg = GetImgFilePath(false);
            System.Drawing.Image img = null;
            if (!File.Exists(strImg))
                return null;
            else
                img = System.Drawing.Image.FromFile(strImg);
            return img;
        }
        // 
        //         public System.Drawing.Image BigImage()
        //         {
        //             string strImg = GetImgFilePath(true);
        //             System.Drawing.Image img = null;
        //             if (!File.Exists(strImg))
        //                 img = Resource.BsCmd_big.ToBitmap();
        //             else
        //                 img = System.Drawing.Image.FromFile(strImg);
        //             return img;
        //         }

    }

    /// <summary>
    /// 一个菜单分组
    /// </summary>
    public class ItemDataGroup
    {
        private MenuItemData Item = new MenuItemData();
        public string Title {
            get
            {
                return Item.Text;
            }
            set
            {
                Item.Text = value;
            }
        }
        private List<MenuItemData> childs = null;
        private List<ItemDataGroup> chaildGroup = null;

        public ItemDataGroup()
        {

        }

        public MenuItemData GetItem()
        {
            return Item;
        }
        public void DoChangeData(XmlNode node, bool bLoad)
        {
            Item.DoChangeData(node, bLoad);
            childs = new List<MenuItemData>();
            chaildGroup = new List<ItemDataGroup>();
            XmlNodeList nodeList = node.ChildNodes;
            foreach (XmlNode nodeItem in nodeList)
            {
                // if (nodeItem.Name)
                if (nodeItem.Name.ToLower() == "cmd")
                {
                    MenuItemData item = new MenuItemData();
                    item.DoChangeData(nodeItem, true);
                    childs.Add(item);
                }
                else if (nodeItem.Name.ToLower() == "cmdgroup")
                {
                    ItemDataGroup group = new ItemDataGroup();
                    chaildGroup.Add(group);
                    group.DoChangeData(nodeItem, bLoad);
                }
            }
        }

        public List<MenuItemData> GetChildLst()
        {
            return childs;
        }
        public List<ItemDataGroup> GetChildGroupLst()
        {
            return chaildGroup;
        }
    }
}
